home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_010 / iff / showilbm.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  266 lines

  1. /** ShowILBM.c **************************************************************
  2.  *
  3.  * Read an ILBM raster image file and display it.  11/19/85.
  4.  *
  5.  * By Jerry Morrison, Steve Shaw, and Steve Hayes, Electronic Arts.
  6.  * This software is in the public domain.
  7.  *
  8.  * USE THIS AS AN EXAMPLE PROGRAM FOR AN IFF READER.
  9.  *
  10.  * The IFF reader portion is essentially a recursive-descent parser.
  11.  * This program will look into a CAT or LIST to find a FORM ILBM, but it
  12.  * won't look inside another FORM type for a nested FORM ILBM.
  13.  *
  14.  * The display portion is specific to the Commodore Amiga computer.
  15.  *
  16.  * NOTE: This program displays an image, pauses, then exits.
  17.  *
  18.  * Usage from CLI:
  19.  *   showilbm picture1 [picture2] ...
  20.  *
  21.  * Usage from WorkBench:
  22.  * Click on ShowILBM, hold down shift key, click on each picture to show,
  23.  * Double-click on final picture to complete the selection, release the
  24.  * shift key.
  25.  *
  26.  ****************************************************************************/
  27.  
  28. #include <libraries/dos.h>
  29. #include <libraries/dosextens.h>
  30. #include <graphics/gfxbase.h>
  31. #include "ilbm.h"
  32. #include <workbench/workbench.h>
  33. #include <workbench/startup.h>
  34. #include "readpict.h"
  35.  
  36. /* ALLOCator that remembers size, allocates in CHIP-accessible memory.
  37.  * Does clear memory being allocated. */
  38. extern UBYTE *ChipAlloc( /* LONG size */ );
  39.  
  40. /*---------- For MANX ---------*/
  41.  
  42. extern IFFP ReadPicture();
  43. extern LONG Open();
  44. extern LONG CurrentDir();
  45. extern void *OpenLibrary();
  46.  
  47. /* FREEs memory allocated by allocators which REMember size allocated.
  48.  * Safe: checks whether pointer is NULL before attempting to free it. */
  49. extern void RemFree( /* UBYTE *address */ );
  50.  
  51. /*  $$$ Manx Mod needed here **/
  52. #define MIN(a,b) ((a)<(b)?(a):(b))
  53.  
  54. /* general usage pointers */
  55. struct GfxBase *GfxBase;
  56. LONG IconBase;  /* Actually, "struct IconBase *" if you've got some ".h" file*/
  57.  
  58. /* For displaying an image */
  59. static struct RastPort rP;
  60. static struct BitMap bitmap0;
  61. static struct RasInfo rasinfo;
  62. static struct View v = {0};
  63. static struct ViewPort vp = {0};
  64.  
  65. static ILBMFrame iFrame;
  66.  
  67. /* Define the size of a temporary buffer used in unscrambling the ILBM rows.*/
  68. #define bufSz 512
  69.  
  70. /* Message strings for IFFP codes. */
  71. static char MsgOkay[]        = { "(IFF_OKAY) No FORM ILBM in the file." };
  72. static char MsgEndMark[]     = { "(END_MARK) How did you get this message?" };
  73. static char MsgDone[]        = { "(IFF_DONE) All done."};
  74. static char MsgDos[]         = { "(DOS_ERROR) The DOS returned an error." };
  75. static char MsgNot[]         = { "(NOT_IFF) Not an IFF file." };
  76. static char MsgNoFile[]      = { "(NO_FILE) No such file found." };
  77. static char MsgClientError[] = { "(CLIENT_ERROR) ShowILBM bug or insufficient RAM."};
  78. static char MsgForm[]        = { "(BAD_FORM) A malformed FORM ILBM." };
  79. static char MsgShort[]       = { "(SHORT_CHUNK) A malformed FORM ILBM." };
  80. static char MsgBad[]         = { "(BAD_IFF) A mangled IFF file." };
  81.  
  82. /* THESE MUST APPEAR IN RIGHT ORDER!! */
  83. static char *IFFPMessages[-LAST_ERROR+1] = {
  84.     /*IFF_OKAY*/  MsgOkay,
  85.     /*END_MARK*/  MsgEndMark,
  86.     /*IFF_DONE*/  MsgDone,
  87.     /*DOS_ERROR*/ MsgDos,
  88.     /*NOT_IFF*/   MsgNot,
  89.     /*NO_FILE*/   MsgNoFile,
  90.     /*CLIENT_ERROR*/ MsgClientError,
  91.     /*BAD_FORM*/  MsgForm,
  92.     /*SHORT_CHUNK*/  MsgShort,
  93.     /*BAD_IFF*/   MsgBad
  94.     };
  95.  
  96. /** DisplayPic() ************************************************************
  97.  *
  98.  * Interface to Amiga graphics ROM routines.
  99.  *
  100.  ****************************************************************************/
  101. DisplayPic(bm, ptilbmFrame)
  102. struct BitMap *bm;
  103. ILBMFrame *ptilbmFrame;
  104. {
  105.     int i;
  106.     struct View *oldView = GfxBase->ActiView;   /* so we can restore it */
  107.  
  108.     InitView(&v);
  109.     InitVPort(&vp);
  110.     v.ViewPort = &vp;
  111.     InitRastPort(&rP);
  112.     rP.BitMap = bm;
  113.     rasinfo.BitMap = bm;
  114.  
  115.     /* Always show the upper left-hand corner of this picture. */
  116.     rasinfo.RxOffset = 0;
  117.     rasinfo.RyOffset = 0;
  118.  
  119.     vp.DWidth  = ptilbmFrame->bmHdr.w;
  120.     vp.DHeight = ptilbmFrame->bmHdr.h;
  121.  
  122. #if 0
  123.     /* Specify where on screen to put the ViewPort. */
  124.     vp.DxOffset = ptilbmFrame->bmHdr.x;
  125.     vp.DyOffset = ptilbmFrame->bmHdr.y;
  126. #else
  127.     /* Always display it in upper left corner of screen.*/
  128. #endif
  129.  
  130.     if (ptilbmFrame->bmHdr.pageWidth <= 320)
  131.         vp.Modes = 0;
  132.     else vp.Modes = HIRES;
  133.     if (ptilbmFrame->bmHdr.pageHeight > 200) {
  134.         v.Modes |= LACE;
  135.         vp.Modes |= LACE;
  136.     }
  137.     vp.RasInfo = &rasinfo;
  138.     MakeVPort(&v,&vp);
  139.     MrgCop(&v);
  140.     LoadView(&v);       /* show the picture */
  141.     WaitBlit();
  142.     WaitTOF();
  143.     LoadRGB4(&vp, ptilbmFrame->colorMap, (long)ptilbmFrame->nColorRegs);
  144.  
  145.     for (i = 0; i < 5*60; ++i)  WaitTOF();      /* Delay 5 seconds. */
  146.  
  147.     LoadView(oldView);  /* switch back to old view */
  148. }
  149.  
  150. /** main0() *****************************************************************/
  151. static struct WBStartup *wbStartup = 0; /* 0 unless started from WorkBench.*/
  152.  
  153. PrintS(msg)
  154. char *msg;
  155. {
  156.     if (!wbStartup)
  157.        printf(msg);
  158. }
  159.  
  160. void GoodBye(msg)
  161. char *msg;
  162. {
  163.     PrintS(msg);
  164.     PrintS("\n");
  165.     exit(0L);
  166. }
  167.  
  168. LONG OpenArg(wa, openmode)
  169. struct WBArg *wa;
  170. long openmode;         /** $$$ Changed from "int" to "long" **/
  171. {
  172.     LONG olddir;
  173.     LONG file;
  174.     if (wa->wa_Lock)
  175.        olddir = CurrentDir(wa->wa_Lock);
  176.  
  177.     file = Open(wa->wa_Name, openmode);
  178.     if (wa->wa_Lock)
  179.        CurrentDir(olddir);
  180.  
  181.     return(file);
  182. }
  183.  
  184. void main0(wa)
  185. struct WBArg *wa;
  186. {
  187.     LONG file;
  188.     IFFP iffp = NO_FILE;
  189.  
  190.     /* load and display the picture */
  191.     file = OpenArg(wa, MODE_OLDFILE);
  192.     if (file)
  193.         iffp = ReadPicture(file, &bitmap0, &iFrame, ChipAlloc);
  194.         /* Allocates BitMap using ChipAlloc().*/
  195.  
  196.     Close(file);
  197.     if (iffp == IFF_DONE)
  198.         DisplayPic(&bitmap0, &iFrame);
  199.  
  200.     PrintS(" ");   PrintS(IFFPMessages[-iffp]);   PrintS("\n");
  201.  
  202.     /* cleanup */
  203.     if (bitmap0.Planes[0])  {
  204.         RemFree(bitmap0.Planes[0]);
  205.  
  206.     /* ASSUMES allocated all planes via a single ChipAlloc call.*/
  207.     FreeVPortCopLists(&vp);
  208.     FreeCprList(v.LOFCprList);
  209.     }
  210. }
  211.  
  212. /** main() ******************************************************************/
  213.  
  214. void main(argc, argv)
  215. int argc;
  216. char **argv;
  217. {
  218.     struct WBArg wbArg, *wbArgs;
  219.     LONG olddir;
  220. /*sss    struct Process *myProcess; */
  221.  
  222.     if( !(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0L)) )
  223.         GoodBye("No graphics.library");
  224.  
  225.     if( !(IconBase = (LONG)OpenLibrary("icon.library",0L)) )
  226.         GoodBye("No icon.library");
  227.  
  228.     if (!argc) {
  229.         /* Invoked via workbench */
  230.         wbStartup = (struct WBStartup *)argv;
  231.         wbArgs = wbStartup->sm_ArgList;
  232.         argc = wbStartup->sm_NumArgs;
  233.         while (argc >= 2) {
  234.             olddir = CurrentDir(wbArgs[1].wa_Lock);
  235.             main0(&wbArgs[1]);
  236.             argc--;   wbArgs = &wbArgs[1];
  237.         }
  238. #ifdef xxxx
  239.         if (argc < 2) {
  240.             PrintS ("Usage from workbench:\n");
  241.             PrintS (" Click mouse on Show-ILBM, Then hold 'SHIFT' key\n");
  242.             GoodBye(" while double-click on file to display.");
  243.         }
  244. #endif
  245.     }
  246.     else {
  247.         /* Invoked via CLI.  Make a lock for current directory.
  248.          * Eventually, scan name, separate out directory reference?*/
  249.         if (argc < 2)
  250.             GoodBye("Usage from CLI: 'Show-ILBM filename'");
  251. /*sss   myProcess = (struct Process *)FindTask(0); */
  252.         wbArg.wa_Lock = 0; /*sss myProcess->pr_CurrentDir; */
  253.         while (argc >= 2) {
  254.             wbArg.wa_Name = argv[1];
  255.             PrintS("Showing file ");   PrintS(wbArg.wa_Name);   PrintS(" ...");
  256.             main0(&wbArg);
  257.             PrintS("\n");
  258.             argc--;   argv = &argv[1];
  259.         }
  260.     }
  261.     CloseLibrary(GfxBase);
  262.     CloseLibrary(IconBase);
  263.     exit(0);
  264. }
  265.  
  266.